package com.xiaoran.springbt02web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class RequestController {
    @GetMapping("/goto")
    public String gotoPage(HttpServletRequest request){
        request.setAttribute("msg","成功了。。。。");
        request.setAttribute("code",20);
        return "forward:/success";   //转发请求到success
    }

    @ResponseBody
    @GetMapping("/success")
    public Map<String,Object> success(HttpServletRequest request, @RequestAttribute("code") String code){
        Map<String,Object> map=new HashMap<>();
        map.put("code",code);
        Object object=request.getAttribute("msg");
        map.put("object",object);
        return map;
    }

    ///cars/sell;low=34;brand=byd,audi,yd
    @ResponseBody
    @GetMapping("/cars/{sell}")
    public Map<String ,Object> carssell(@MatrixVariable("low") Integer low,
                                        @MatrixVariable("brand")List<String> brand,
                                        @PathVariable("sell") String path){
        Map<String,Object> map=new HashMap<>();
        map.put("low",low);
        map.put("brand",brand);
        map.put("path",path);
        return map;
    }

    ///boss/1;age=20/2;age=30
    @ResponseBody
    @GetMapping("/boss/{id1}/{id2}")
    public Map<String,Object> boss(@MatrixVariable(value = "age",pathVar = "id1"/*pathVar这就用来解决我们一个路径下重名问题*/) Integer age1,
                                   @MatrixVariable(value = "age" ,pathVar = "id2") Integer age2,
                                   @PathVariable("id1") Integer pa){
        Map<String,Object> map=new HashMap<>();
        map.put("age1",age1);
        map.put("age2",age2);
        map.put("pa",pa);

        return map;
    }

}
